package com.practice.springboot_01_zwj.controller;

import com.github.pagehelper.PageInfo;
import com.practice.springboot_01_zwj.entity.CloudNetwork;
import com.practice.springboot_01_zwj.entity.vo.R;
import com.practice.springboot_01_zwj.service.CloudNetworkService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/cloudNetwork")
public class CloudNetworkController {

    @Autowired
    private CloudNetworkService cloudNetworkService;

    @GetMapping("/selectByCityCode")
    public PageInfo<CloudNetwork> selectByCityCode(
            @RequestParam(required = false, defaultValue = "") String cityCode, // 添加默认值
            @RequestParam(required = false, defaultValue = "") String providerType, // 添加默认值
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size
    ) {
        return cloudNetworkService.selectByCityCode(cityCode, providerType,page, size);
    }

    /**
     * 政务外网客户清单增删改查
     * @param cloudNetwork
     * @return
     */
    @PostMapping("/addNetworkCustomer")
    public R<Integer> addNetworkCustomer(@RequestBody CloudNetwork cloudNetwork){
        try {
            int result = cloudNetworkService.addNetworkCustomer(cloudNetwork);
            if(result > 0){
                return R.success("添加成功",cloudNetwork.getId());
            }else {
                return R.error("添加失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }
    @PutMapping("/updateNetworkCustomer")
    public R<String> updateNetworkCustomer(@RequestBody CloudNetwork cloudNetwork){
        try {
            if (cloudNetwork.getId() == null){
                return R.error(400,"更新失败，任务ID不能为空");
            }
            int result = cloudNetworkService.updateNetworkCustomer(cloudNetwork);
            if(result > 0){
                return R.success("更新成功",null);
            }else {
                return R.error("更新失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }
    @DeleteMapping("/deleteNetworkCustomer/{id}")
    public R<String> deleteNetworkCustomer(@PathVariable Integer id){
        try {
            int result = cloudNetworkService.deleteNetworkCustomer(id);
            if(result > 0){
                return R.success("删除成功",null);
            }else {
                return R.error("删除失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }

    @GetMapping("/selectByCityCodeAndCustomerType")
    public PageInfo<CloudNetwork> selectByCityCodeAndCustomerType(
            @RequestParam(required = false) String cityCode,
            @RequestParam(required = false) String customerType,
            @RequestParam(required = false) String districtName,
            @RequestParam(required = false) String customerName,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size
    ) {
        return cloudNetworkService.selectByCityCodeAndCustomerType(cityCode, customerType, districtName,customerName,page, size);
    }

    // 新增的统计接口
    @GetMapping("/customerTypeCount")
    public ResponseEntity<?> getCityStats() {
        try {
            List<CityStatsDTO> result = cloudNetworkService.getCityStats();
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.status(500).body(
                    new ErrorResponse(500, "获取统计信息失败: " + e.getMessage())
            );
        }
    }
    //下拉区县
    @GetMapping("/selectDistrictName")
    public List<CloudNetwork> selectDistrictName(String cityCode) {
        return cloudNetworkService.selectDistrictName(cityCode);
    }

    /**
     * 政务外网商机清单增删改查
     * @param cloudNetwork
     * @return
     */
    @PostMapping("/addNetworkShangJi")
    public R<Integer> addNetworkShangJi(@RequestBody CloudNetwork cloudNetwork){
        try {
            int result = cloudNetworkService.addNetworkShangJi(cloudNetwork);
            if(result > 0){
                return R.success("添加成功",cloudNetwork.getId());
            }else {
                return R.error("添加失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }
    @PutMapping("/updateNetworkShangJi")
    public R<String> updateNetworkShangJi(@RequestBody CloudNetwork cloudNetwork){
        try {
            if (cloudNetwork.getId() == null){
                return R.error(400,"更新失败，任务ID不能为空");
            }
            int result = cloudNetworkService.updateNetworkShangJi(cloudNetwork);
            if(result > 0){
                return R.success("更新成功",null);
            }else {
                return R.error("更新失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }
    @DeleteMapping("/deleteNetworkShangJi/{id}")
    public R<String> deleteNetworkShangJi(@PathVariable Integer id){
        try {
            int result = cloudNetworkService.deleteNetworkShangJi(id);
            if(result > 0){
                return R.success("删除成功",null);
            }else {
                return R.error("删除失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }
    @GetMapping("/selectByCityCodeAndProviderType")
    public PageInfo<CloudNetwork> selectByCityCodeAndProviderType(
            @RequestParam(required = false, defaultValue = "") String cityCode,
            @RequestParam(required = false, defaultValue = "") String providerType,
            @RequestParam(required = false, defaultValue = "") String districtName,
            @RequestParam(required = false, defaultValue = "false") Boolean filterByContractEndTime,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {

        return cloudNetworkService.selectByCityCodeAndProviderType(
                cityCode, providerType, districtName, filterByContractEndTime, page, size
        );
    }
    @GetMapping("/countShangJiByCityCode")
    public List<Map<String, Object>> countShangJiByCityCode() {
        return cloudNetworkService.countShangJiByCityCode();
    }

    @GetMapping("/countByFourLevelClassify")
    public List<Map<String, Object>> countByFourLevelClassify(
            @RequestParam(required = false, defaultValue = "false") Boolean filterByContractEndTime,
            @RequestParam(required = false, defaultValue = "") String providerType) {
        return cloudNetworkService.countByFourLevelClassify(filterByContractEndTime,providerType);
    }

    /**
     *政务云客户清单增删改查
     */
    @PostMapping("/addCloudCustomerType")
    public R<Integer> addCloudCustomerType(@RequestBody CloudNetwork cloudNetwork){
        try {
            int result = cloudNetworkService.addCloudCustomerType(cloudNetwork);
            if(result > 0){
                return R.success("添加成功",cloudNetwork.getId());
            }else {
                return R.error("添加失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }
    @PutMapping("/updateCloudCustomerType")
    public R<String> updateCloudCustomerType(@RequestBody CloudNetwork cloudNetwork){
        try {
            if (cloudNetwork.getId() == null){
                return R.error(400,"更新失败，任务ID不能为空");
            }
            int result = cloudNetworkService.updateCloudCustomerType(cloudNetwork);
            if(result > 0){
                return R.success("更新成功",null);
            }else {
                return R.error("更新失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }
    @DeleteMapping("/deleteCloudCustomerType/{id}")
    public R<String> deleteCloudCustomerType(@PathVariable Integer id){
        try {
            int result = cloudNetworkService.deleteCloudCustomerType(id);
            if(result > 0){
                return R.success("删除成功",null);
            }else {
                return R.error("删除失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }
    @GetMapping("/selectCloudCustomerType")
    public PageInfo<CloudNetwork> selectCloudCustomerType(
            @RequestParam(required = false, defaultValue = "") String cityCode, // 添加默认值
            @RequestParam(required = false, defaultValue = "") String customerType, // 添加默认值
            @RequestParam(required = false, defaultValue = "") String providerType, // 添加默认值
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size
    ) {
        return cloudNetworkService.selectCloudCustomerType(cityCode,customerType,providerType,page, size);
    }

    @GetMapping("/countByCustomerType")
    public List<Map<String, Object>> countByCustomerType() {
        return cloudNetworkService.countByCustomerType();
    }

    /**
     *政务云商机清单的增删改查
     */
    @PostMapping("/addCloudComplete")
    public R<Integer> addCloudComplete(@RequestBody CloudNetwork cloudNetwork){
        try {
            int result = cloudNetworkService.addCloudComplete(cloudNetwork);
            if(result > 0){
                return R.success("添加成功",cloudNetwork.getId());
            }else {
                return R.error("添加失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }
    @PutMapping("/updateCloudComplete")
    public R<String> updateCloudComplete(@RequestBody CloudNetwork cloudNetwork){
        try {
            if (cloudNetwork.getId() == null){
                return R.error(400,"更新失败，任务ID不能为空");
            }
            int result = cloudNetworkService.updateCloudComplete(cloudNetwork);
            if(result > 0){
                return R.success("更新成功",null);
            }else {
                return R.error("更新失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }
    @DeleteMapping("/deleteCloudComplete/{id}")
    public R<String> deleteCloudComplete(@PathVariable Integer id){
        try {
            int result = cloudNetworkService.deleteCloudComplete(id);
            if(result > 0){
                return R.success("删除成功",null);
            }else {
                return R.error("删除失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }
    @GetMapping("/selectCloudComplete")
    public PageInfo<CloudNetwork> selectCloudComplete(
            @RequestParam(required = false, defaultValue = "") String cityCode, // 添加默认值
            @RequestParam(required = false, defaultValue = "") String providerType, // 添加默认值
            @RequestParam(required = false, defaultValue = "false") Boolean filterByContractEndTime,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size
    ) {
        return cloudNetworkService.selectCloudComplete(cityCode,providerType,filterByContractEndTime,page, size);
    }
    @GetMapping("/countCloudCompleteByCityCode")
    public List<Map<String, Object>> countCloudCompleteByCityCode() {
        return cloudNetworkService.countCloudCompleteByCityCode();
    }
    @GetMapping("/countCloudCompleteByContractEndTime")
    public List<Map<String, Object>> countCloudCompleteByContractEndTime(
            @RequestParam(required = false) String cityCode,
            @RequestParam(required = false) String providerType,
            @RequestParam(required = false, defaultValue = "false") Boolean filterByContractEndTime
    ) {
        return cloudNetworkService.countCloudCompleteByContractEndTime(cityCode,providerType, filterByContractEndTime);
    }

    @Data
    public static class CityStatsDTO {
        private String cityCode;
        private Integer totalCount;
        private List<CustomerTypeStats> customerTypeStats;
        private List<DistrictStats> districtStats;

        public String getCityCode() {
            return cityCode;
        }

        public void setCityCode(String cityCode) {
            this.cityCode = cityCode;
        }

        public Integer getTotalCount() {
            return totalCount;
        }

        public void setTotalCount(Integer totalCount) {
            this.totalCount = totalCount;
        }

        public List<CustomerTypeStats> getCustomerTypeStats() {
            return customerTypeStats;
        }

        public void setCustomerTypeStats(List<CustomerTypeStats> customerTypeStats) {
            this.customerTypeStats = customerTypeStats;
        }

        public List<DistrictStats> getDistrictStats() {
            return districtStats;
        }

        public void setDistrictStats(List<DistrictStats> districtStats) {
            this.districtStats = districtStats;
        }
    }
    @Data
    public static class CustomerTypeStats {
        private String customerType;
        private Integer count;

        public String getCustomerType() {
            return customerType;
        }

        public void setCustomerType(String customerType) {
            this.customerType = customerType;
        }

        public Integer getCount() {
            return count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }
    }
    @Data
    public static class DistrictStats {
        private String districtName;
        private Integer totalCount;
        private List<CustomerTypeStats> customerTypeStats;

        public String getDistrictName() {
            return districtName;
        }

        public void setDistrictName(String districtName) {
            this.districtName = districtName;
        }

        public Integer getTotalCount() {
            return totalCount;
        }

        public void setTotalCount(Integer totalCount) {
            this.totalCount = totalCount;
        }

        public List<CustomerTypeStats> getCustomerTypeStats() {
            return customerTypeStats;
        }

        public void setCustomerTypeStats(List<CustomerTypeStats> customerTypeStats) {
            this.customerTypeStats = customerTypeStats;
        }
    }
    @Data
    public static class ErrorResponse {
        private int code;
        private String message;

        public ErrorResponse(int code, String message) {
            this.code = code;
            this.message = message;
        }
    }
}

